home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
BUS
/
TMCM Software and Labs.sit
/
Software for TMCM 7_95
/
Files for Lab 13
/
3N+1 Max
next >
Wrap
Text File
|
1995-07-08
|
1KB
|
44 lines
{
This program finds the length of the longest 3N+1
sequence for all starting values of N less than or
equal to 30.
The program uses multitasking. Thirty turtles are
created. Each one computes a different 3N+1 sequence,
and these computations all take place simultaneously.
}
DECLARE max { The length of the longest sequence }
DECLARE StartValueForMax { Starting value for that sequence }
SUB three_n_max(values)
IMPORT max, StartValueForMax { The subroutine computes values for }
max := 0 { these two global variables. }
fork(values)
DECLARE n
DECLARE ct
n := ForkNumber
ct := 1
LOOP
EXIT IF n = 1
ct := ct + 1
IF n/2 = trunc(n/2) THEN
n := n/2
ELSE
n := n*3 + 1
END IF
END LOOP
GRAB max THEN { Note that only global variables can be used in GRAB statements. }
IF ct > max THEN
max := ct
StartValueForMax := ForkNumber
END IF
END GRAB
END SUB
three_n_max(30)
TellUser("The longest sequence was #max steps long, and it occured for a starting value of #StartValueForMax.")